/*
 * main.cpp
 *
 *  Created on: 6 mei 2013
 *      Author: Wietse
 */


#include "main.h"
#include "Swarm.h"

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <ctime>


using namespace std;

/* Prototypes */
void init();
void update();
void display();

// Generate swarm object so constructor is only called once
Swarm swarm = Swarm();
int tlast;



/**
 *  Initialize the OpenGL state
 */
void init() {
	glClearColor( 1.0, 1.0, 1.0, 1.0 ); /* Set the clear color */

	glViewport(xvp, yvp, wvp, hvp);
	gluOrtho2D (xwmin, xwmax, zwmin, zwmax);
}

/**
 * Update the simulation
 */
void update()
{
	// Calculate the elapsed time
	int tnew = glutGet(GLUT_ELAPSED_TIME);
	double deltaTime = (double) (tnew - tlast)/1000;
	tlast = tnew;

	// Update the swarm
	swarm.update(deltaTime);

	// Call the display function so the screen is updated
	display();
}

/**
 * Displays the simulation
 */
void display()
{
	// Clear the screen with the clear color
	glClear( GL_COLOR_BUFFER_BIT );

	// Display the Swarm
	swarm.display();

	// Double buffering
	glutSwapBuffers();
}

/**
 * Main function
 */
int main(int argc, char *argv[] )
{
	cout << "Start Simulation" << endl;

	//Glut setup function calls
	glutInit( &argc, argv );

	// Set window properties
	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB ); /* Use double buffering and RGB colors */
	glutInitWindowPosition( 0, 0 );
	glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT );

	// Create window
	glutCreateWindow( "Swarm Simulator" );
	init();

	// Set callback functions and initiate call back loop
	glutDisplayFunc( display );
	glutIdleFunc( update );

	// Store time
	tlast = glutGet(GLUT_ELAPSED_TIME);

	// Start loop
	glutMainLoop();

	return 0;
}

